package de.codecasa.andro.kasse;



import java.util.ArrayList;
import java.util.List;

import de.codecasa.andro.kasse.NumKeypad.Tasten;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;


 
public class AC_kasse extends Activity {
		
	//private static final int MY_BUTTON = 9000;
	TableLayout tlKasse;	


	

	LinearLayout llRight;
	
		

	private dbActions datasource;
	private List<dsrArtikel> ArtikelList = new ArrayList<dsrArtikel>();
	private List<dsrBuchung> BuchungenList = new ArrayList<dsrBuchung>();
		
	private ProductKeypad pKeypad;
	private NumKeypad nKeypad;
	private DisplayKlein display;

	public void onResume()
	{
		super.onResume();
		pKeypad.updateButtons();	
	}
	
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

                
                        
        setContentView(R.layout.kasse_layout);

        datasource = new dbActions(this);
        
        tlKasse = (TableLayout)findViewById(R.id.tlKasse);   
        
        display = new DisplayKlein(this);
       
        
        kaCE();
        
       
        nKeypad = new NumKeypad(this, display);
        
        nKeypad.setOnButtonClickEventListener(new NumKeypad.NumEventListener() {
			
			@Override
			public void onTasteClick(Tasten taste) {
				// TODO Auto-generated method stub
				
				switch(taste)
				{
					case CE:
						kaCE();
				}
				
			}
		});  
        	
      

        pKeypad = new ProductKeypad(tlKasse, this, datasource);
        pKeypad.setOnAddProductEventListener(new EventListener() {
			@Override
			public void onAddProduct(Integer productID) {
				//  Auto-generated method stub
				buchen(productID);
			}

          });
                
   
    }
    
	    
    
    private void buchen(int id)
    {
       	try 
       	{
        	datasource.open();
        	
        	int anz = nKeypad.getAnzahl();
        	if(anz < 1) anz = 1;
        	
        	for(int ai = 0; ai < anz; ai ++)
        	{
            	datasource.createBuchung(1,1, id, 1);
        	}
        	datasource.close();
        	
            datasource.open();
            ArtikelList = datasource.getArtikel("ANr=" + id);
            datasource.close();
     	
            
              		
            
     		
            if(ArtikelList.size() > 0)
            {
	            for (dsrArtikel artikel : ArtikelList) {
	            	for(int ai = 0; ai < anz; ai ++)
	            	{
		     			//summe = summe.add(artikel.getpreis());
		     			display.addBuchungsbetrag(artikel.getpreis());
	            	}
	    		}

            }
        	
        }
        catch (Exception ex) 
        {
        		Toast.makeText(this,ex.toString(), Toast.LENGTH_LONG).show();
        }    	
       	
       	
       	nKeypad.clearNumpad();
       	
       	
    	
    }
    
    
    private void fill_Buchungen()
    {
    	datasource.open();
    	BuchungenList = datasource.getBuchungen(null);
    	datasource.close();
    	
    	TableLayout tlBuchungen = (TableLayout)findViewById(R.id.tlBuchungen);  
    	tlBuchungen.removeAllViews();
    	
    	
    	if (BuchungenList.size() > 0) 
    	{
	    	for(dsrBuchung buchung : BuchungenList)
	    	{
	    		TableRow nRow = new TableRow(this);
	    		
	    		TextView tv1 = new TextView(this);
	    		tv1.setText(String.format("%d",buchung.getId()));
	    		tv1.setBackgroundColor(Color.GRAY);
	    		nRow.addView(tv1);

	    		TextView tv2 = new TextView(this);
	    		tv2.setText(String.format("%d",buchung.getBNr()));
	    		tv2.setBackgroundColor(Color.BLUE);
	    		nRow.addView(tv2);

	    		TextView tv3 = new TextView(this);
	    		tv3.setText(String.format("%d",buchung.getANr()));
	    		tv3.setBackgroundColor(Color.RED);
	    		nRow.addView(tv3);

	    		TextView tv4 = new TextView(this);
	    		tv4.setText(String.format("%d",buchung.getanzahl()));
	    		tv4.setBackgroundColor(Color.GREEN);
	    		nRow.addView(tv4);
	    		
	    		tlBuchungen.addView(nRow);
	    	}
    	}
    	
    }
 
    
    
	private void kaCE()
	{
			
		datasource.open();
		datasource.removerBuchungen();
		datasource.close();              
			
		display.clearDisplay();
		fill_Buchungen();
	}
    

	
 	
 	


	
 	
 	
 	
	


	
}
	
